package com.rt.easyjava.base.biz1.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.easyjava.base.biz1.bean.TmptAuthCompanyBiz;
import com.rt.easyjava.base.biz1.dto.TmptAuthCompanyBizDto;
import com.rt.easyjava.base.biz1.service.intf.TmptAuthCompanyBizService;
import com.rt.tmpt.service.utils.SpringContextHolder;
import com.rt.easyjava.common.base.MyQueryWrapper;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.exception.ControllerException;
import com.rt.easyjava.common.utils.others.DateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

import java.lang.reflect.Method;

/**
 * 前端控制器
 * @author wuxiaoyong
 * @since 2020-06-06
 * 用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，_userJson)
 */
public class TmptAuthCompanyController extends ControllerException {
    private final Logger logger = LoggerFactory.getLogger(TmptAuthCompanyController.class);

    @Autowired
    protected MyMapper mapper;
    @Autowired
    protected TmptAuthCompanyBizService tmptAuthCompanyBizService;


    protected Result page(TmptAuthCompanyBizDto tmptAuthCompanyBizDto){
        Result<TmptAuthCompanyBizDto> result = new Result<>();

        Page<TmptAuthCompanyBiz> page = new Page<>(tmptAuthCompanyBizDto.getPage(), tmptAuthCompanyBizDto.getLimit());

        TmptAuthCompanyBiz tmptAuthCompanyBiz = mapper.map(tmptAuthCompanyBizDto, TmptAuthCompanyBiz.class);
        if(tmptAuthCompanyBiz.getDeleteflag()==null){
            tmptAuthCompanyBiz.setDeleteflag(0);
        }

        QueryWrapper<TmptAuthCompanyBiz> queryWrapper=new QueryWrapper<>(tmptAuthCompanyBiz);
        //特殊查询条件
        queryWrapper.gt(tmptAuthCompanyBizDto.getCreatedate_Start()!=null,"createdate",tmptAuthCompanyBizDto.getCreatedate_Start());
        queryWrapper.lt(tmptAuthCompanyBizDto.getCreatedate_End()!=null,"createdate",tmptAuthCompanyBizDto.getCreatedate_End());
        queryWrapper.orderBy(tmptAuthCompanyBizDto.getCondition(), tmptAuthCompanyBizDto.isAsc(), tmptAuthCompanyBizDto.getColumn());

        return tmptAuthCompanyBizService.page(page, queryWrapper);
    }

    /**
        树结构查询方法，非树结构会造一个空方法备用着。
    */
    protected Result tree (Long id) {
        Result<TmptAuthCompanyBizDto> result = new Result<>();
        // 树递归
        TmptAuthCompanyBiz tmptAuthCompanyBiz = tmptAuthCompanyBizService.getById(id);
        TmptAuthCompanyBizDto tmptAuthCompanyBizDto = mapper.map(tmptAuthCompanyBiz, TmptAuthCompanyBizDto.class);
        treeReduce(tmptAuthCompanyBizDto);
        result.setObj(tmptAuthCompanyBizDto);

        return result.success();
    }

    private void treeReduce(TmptAuthCompanyBizDto tmptAuthCompanyBizDto){
    }

    protected Result myPage(TmptAuthCompanyBizDto tmptAuthCompanyBizDto){
        Result<TmptAuthCompanyBizDto> result = new Result<>();

        Page<TmptAuthCompanyBiz> page = new Page<>(tmptAuthCompanyBizDto.getPage(), tmptAuthCompanyBizDto.getLimit());

        TmptAuthCompanyBiz tmptAuthCompanyBiz = mapper.map(tmptAuthCompanyBizDto, TmptAuthCompanyBiz.class);
        if(tmptAuthCompanyBiz.getDeleteflag()==null){
            tmptAuthCompanyBiz.setDeleteflag(0);
        }

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<TmptAuthCompanyBiz> queryWrapper = MyQueryWrapper.buildQueryWrapper(tmptAuthCompanyBiz , "createdate");
        //特殊查询条件
        queryWrapper.gt(tmptAuthCompanyBizDto.getCreatedate_Start()!=null,"t1.createdate",tmptAuthCompanyBizDto.getCreatedate_Start());
        queryWrapper.lt(tmptAuthCompanyBizDto.getCreatedate_End()!=null,"t1.createdate",tmptAuthCompanyBizDto.getCreatedate_End());
        queryWrapper.orderBy(tmptAuthCompanyBizDto.getCondition(), tmptAuthCompanyBizDto.isAsc(), "t1."+tmptAuthCompanyBizDto.getColumn());

        result = tmptAuthCompanyBizService.myPage(page, queryWrapper);

        return result;
    }

    protected Result selectBy(TmptAuthCompanyBizDto tmptAuthCompanyBizDto){
        Result<TmptAuthCompanyBizDto> result = new Result<>();

        TmptAuthCompanyBiz tmptAuthCompanyBiz = mapper.map(tmptAuthCompanyBizDto, TmptAuthCompanyBiz.class);
        if(tmptAuthCompanyBiz.getDeleteflag()==null){
            tmptAuthCompanyBiz.setDeleteflag(0);
        }

        QueryWrapper<TmptAuthCompanyBiz> queryWrapper=new QueryWrapper<>(tmptAuthCompanyBiz);
        //特殊查询条件
        queryWrapper.gt(tmptAuthCompanyBizDto.getCreatedate_Start()!=null,"createdate",tmptAuthCompanyBizDto.getCreatedate_Start());
        queryWrapper.lt(tmptAuthCompanyBizDto.getCreatedate_End()!=null,"createdate",tmptAuthCompanyBizDto.getCreatedate_End());
        queryWrapper.orderBy(tmptAuthCompanyBizDto.getCondition(), tmptAuthCompanyBizDto.isAsc(), tmptAuthCompanyBizDto.getColumn());

        result = tmptAuthCompanyBizService.selectBy(queryWrapper);

        return result;
    }

    protected Result selectById(Long id){
        //调用服务
        return tmptAuthCompanyBizService.selectById(id);
    }

    protected Result select4column(String columnName){
        try {
            Object o = SpringContextHolder.getBean("tmptAuthCompanyBizService");
            Method method = o.getClass().getMethod("mySelect4"+columnName);
            return Result.builder(method.invoke(o)).success();
        }catch (Exception e){
            throw new RuntimeException("方法名不存在");
        }
    }

    protected Result create(@RequestBody TmptAuthCompanyBizDto tmptAuthCompanyBizDto,
                    String _userId, String _userName, String _companyId){
        //插入参数
        tmptAuthCompanyBizDto.setCreatemanId(Long.parseLong(_userId));
        tmptAuthCompanyBizDto.setCreateman(_userName);
        tmptAuthCompanyBizDto.setCreatedate(DateTool.getCurrentDatetimeStr());
        tmptAuthCompanyBizDto.setCompanyId(Long.parseLong(_companyId));

        //调用服务
        return tmptAuthCompanyBizService.create(tmptAuthCompanyBizDto);
    }

    protected Result update(@RequestBody TmptAuthCompanyBizDto tmptAuthCompanyBizDto,
                        String _userId, String _userName){
        //参数
        tmptAuthCompanyBizDto.setModifymanId(Long.parseLong(_userId));
        tmptAuthCompanyBizDto.setModifyman(_userName);
        tmptAuthCompanyBizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptAuthCompanyBizDto.setCreatedate(null);
        tmptAuthCompanyBizDto.setCreateman(null);
        tmptAuthCompanyBizDto.setCreatemanId(null);
        tmptAuthCompanyBizDto.setCompanyId(null);

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", tmptAuthCompanyBizDto.getId()==null?"0":tmptAuthCompanyBizDto.getId().longValue());

        //调用服务
        return tmptAuthCompanyBizService.update(tmptAuthCompanyBizDto, updateWrapper);
    }

    protected Result delete(Long id, String _userId, String _userName){
        Result result = new Result();

        //参数
        TmptAuthCompanyBizDto tmptAuthCompanyBizDto = new TmptAuthCompanyBizDto();
        tmptAuthCompanyBizDto.setModifymanId(Long.parseLong(_userId));
        tmptAuthCompanyBizDto.setModifyman(_userName);
        tmptAuthCompanyBizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptAuthCompanyBizDto.setDeleteflag("1");

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id==null?0:id.longValue());

        //调用服务
        return tmptAuthCompanyBizService.update(tmptAuthCompanyBizDto, updateWrapper);
    }

    protected Result deletePhysical(Long id){
        Result result = new Result();

        //调用服务
        Boolean aBoolean =  false;//tmptAuthCompanyBizService.removeById(id==null?0:id.longValue());
        if(aBoolean){
            result.success();
        }else {
            result.fault();
        }

        return result;
    }

}
